﻿using System.Linq.Expressions;

namespace EmbeddedLanguage.SubProcessors
{
    public class RangeJSProcessor : ExpressionToJSProcessor
    {

        public string RangeVarName;
        public string CounterName;

        

        protected override Expression VisitListInit(ListInitExpression node)
        {
            var incoming = this.CurrentNestedString;
            this.CounterName = this.CreateNewVariableName();
            var ns = new NestedString
            {
                Parent = incoming,
                Header = "for(var {i} = {r}.Start; {i} <= {r}.End; {i}++){".Replace("{i}", this.CounterName).Replace("{r}", this.RangeVarName),
                Footer = "}",
                Comment = "RangeJSProcessor",
            };
            this.CurrentNestedString = ns;
            foreach (ElementInit elemtInit in node.Initializers)
            {
                foreach (var arg in elemtInit.Arguments)
                {
                    var jsFunctionExtractor = new JSFunctionExtractor(this.RangeVarName);
                    this.TransferState(jsFunctionExtractor);
                    jsFunctionExtractor.Visit(arg);
                }
            }
            return node;
        }

        
    }
}
